package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Assign;
import com.example.springboot.entity.User;
import com.example.springboot.service.AssignService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.springboot.common.LogType.BATCH_DELETE;

@RestController
@RequestMapping("/assign")
public class AssignController {

    @Autowired
    AssignService assignService;

    /**
     * 新增信息
     */
    @HoneyLogs(operation ="点检工作情况", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody Assign assign) {
        User currentUser = TokenUtils.getCurrentUser();  //获取当前登录的用户的信息
        assign.setDate(DateUtil.today());   //  2023-10-29
        assignService.save(assign);
        return Result.success();
    }

    /**
     * 修改信息
     */
    @HoneyLogs(operation ="点检工作情况", type = LogType.UPDATE)

    @PutMapping("/update")
    public Result update(@RequestBody Assign assign) {
        assignService.updateById(assign);
        return Result.success();
    }

    /**
     * 删除信息
     */
    @HoneyLogs(operation ="点检工作情况", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        assignService.removeById(id);
        return Result.success();
    }
    /**
     * 批量删除信息
     */
    @HoneyLogs(operation ="点检工作情况", type = BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        assignService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 查询全部信息
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Assign> assignList = assignService.list(new QueryWrapper<Assign>());
        return Result.success(assignList);
    }


    /**
     * 根据ID查询信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Assign assign = assignService.getById(id);
        return Result.success(assign);
    }


    /**
     * 多条件模糊查询信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String name,
                               @RequestParam String type,
                               @RequestParam String date) {
        QueryWrapper<Assign> queryWrapper = new QueryWrapper<Assign>();
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        queryWrapper.like(StrUtil.isNotBlank(type), "type", type);
        queryWrapper.like(StrUtil.isNotBlank(date), "date", date);
        Page<Assign> page = assignService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Assign> records = page.getRecords();
        return Result.success(page);
    }


}

